<?php

/* VIP用户 */
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class VipuserController extends AdminBaseController
{
    public function index()
    {
        $data = $this->request->param();
        $map=[];

        $uid=isset($data['uid']) ? $data['uid']: '';
        if($uid!=''){
            $map[]=['uid','=',$uid];
        }
        
        
        $list = Db::name('vip_user')
            ->where($map)
            ->order("addtime desc")
            ->paginate(20);
        
        $list->each(function($v,$k){
            $userinfo=getUserInfo($v['uid']);
            $v['userinfo']=$userinfo;
            return $v;
        });
        $list->appends($data);
        
        $page = $list->render();
        $this->assign("page", $page);
            
        $this->assign('list', $list);

        return $this->fetch();
    }


    public function add()
    {
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();
            
            $uid=$data['uid'];
            
            
            if($uid < 0){
                $this->error('请填写正确的用户ID');
            }else{
                $check = Db::name('user')->where("id='{$uid}'")->find();
                if(!$check){
                    $this->error('该用户不存在');
                }
                $check = Db::name('vip_user')->where("uid='{$uid}'")->find();
                if($check){
                    $this->error('该用户已购买过VIP，请编辑');
                }
            }
            
            $end=$data['end'];
            if($end==''){
                $this->error('请输入到期时间');
            }
            
            $endtime=strtotime($end);
            
            if(!$endtime){
                $this->error('请输入正确的到期时间');
            }
            
            $insert=[
                'uid'=>$uid,
                'addtime'=>time(),
                'endtime'=>$endtime,
            ];

            $id = DB::name('vip_user')->insert($insert);
            if(!$id){
                $this->error("添加失败！");
            }
            $this->resetcache();
            $this->success("添加成功！");
        }
    }

    public function edit()
    {
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('vip_user')
            ->where("uid={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        
        $userinfo=getUserInfo($data['uid']);
        
        $this->assign('data', $data);
        $this->assign('userinfo', $userinfo);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();
            
            $uid=$data['uid'];

            $end=$data['end'];
            if($end==''){
                $this->error('请输入到期时间');
            }
            
            $endtime=strtotime($end);
            
            if(!$endtime){
                $this->error('请输入正确的到期时间');
            }
            
            $insert=[
                'uid'=>$uid,
                'addtime'=>time(),
                'endtime'=>$endtime,
            ];

            $rs = DB::name('vip_user')->update($insert);

            if($rs === false){
                $this->error("保存失败！");
            }
            $this->resetcache();
            $this->success("保存成功！");
        }
    }
    
    public function listOrder()
    {
        $model = DB::name('vip_user');
        parent::listOrders($model);
        //$this->resetcache();
        $this->success("排序更新成功！");
    }

    public function del()
    {
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = DB::name('vip_user')->where("uid={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->resetcache();
        $this->success("删除成功！",url("Vipuser/index"));
    }


    protected function resetcache(){
        
    }
}